home *** CD-ROM | disk | FTP | other *** search
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include "getopt.h"
-
- char *optarg;
- int optind = 1, opterr = 1;
-
- int getopt(int argc, char *argv[], char *optstring)
- {
- char *argvstr = argv[optind];
- char *optpos;
-
- if ((optind >= argc) ||
- (!strcmp(argvstr, "--")))
- return -1;
- else {
- optind++;
- if (*argvstr != '-')
- return getopt(argc, argv, optstring);
- else
- argvstr++;
- if (*argvstr == '\0')
- return '?';
- optpos = strchr(optstring, *argvstr);
- if (optpos == NULL) {
- if (opterr) fprintf(stderr, "Incorrect command-line options.\n");
- return '?';
- }
- optpos++;
- if (*optpos == ':') {
- if (optind >= argc)
- return '?';
- optarg = argv[optind];
- optind++;
- }
- return (*argvstr);
- }
- }
-